
import 'package:flutter/material.dart';
import 'package:yp/helper.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'dart:async';

import 'state.dart';

class ApplyKocLogic extends GetxController {
  final state = ApplyKocState();

  @override
  void onInit() {
    super.onInit();
    state.nameTextController = TextEditingController();
    state.phoneTextController = TextEditingController();
    state.verifyTextController = TextEditingController();
    state.invitePhoneTextController = TextEditingController();

    state.choosePrivacyAgreement = false;
    state.getVerifyCodeBool = false;
    state.applyKocBool = false;

    update();
  }

  // 隐私协议 选择事件
  void choosePrivacyAgreement(bool chooseBool) {
    state.choosePrivacyAgreement = chooseBool;
    print('--------  choosePrivacyAgreement ');
    if (state.phoneTextController.text != '' && state.nameTextController.text != '' && state.verifyTextController.text != '' && state.choosePrivacyAgreement) {
      state.applyKocBool = true;
    } else {
      state.applyKocBool = false;
    }
    update();
  }
  // 输入框内容改变 事件
  void textControllerClick() {
    if (state.phoneTextController.text != '' && state.nameTextController.text != '' && state.verifyTextController.text != '' && state.choosePrivacyAgreement) {
      state.applyKocBool = true;
    } else {
      state.applyKocBool = false;
    }
    if (state.phoneTextController.text.length != 11) {
      state.getVerifyCodeBool = false;
    }else {
      state.getVerifyCodeBool = true;
    }
    update();
  }

  // 获取验证码
  void getVerifyCode() {
    if (state.getVerifyCodeBool == false) {
      toast('请先输入手机号');
      return;
    }
      var params = {
        "verify_code_type" : "signup",
        "mobile" : state.phoneTextController.text
      };
      Api.sendSms(params).listen((event) {
        if (event["code"] != 0) {
          toast(event["message"]);
        }else  {
          state.smsData = event;
          toast("验证码已发送,请注意查收");
          update();
        }
      });

      state.timer = Timer.periodic(Duration(seconds: 1), (timer) {
        if (state.seconds == 0) {
          state.timer?.cancel();
          state.verifyStr = '重新发送';
          state.seconds = 60;
          update();
          return;
        }
        state.seconds = --state.seconds;
        state.verifyStr = "已发送(${state.seconds}s)";
        print("已发送(${state.seconds}s)");
        update();
      });
  }

  // 申请koc
  void onSubmit() {
    if (state.choosePrivacyAgreement == false) {
      toast('请先阅读并同意勾选服务协议');
      return;
    }
    if (state.nameTextController.text == '') {
      toast('请输入姓名');
      return ;
    }
    if (state.phoneTextController.text == '') {
      toast('请输入手机号');
      return ;
    }
    if (state.verifyTextController.text == '') {
      toast('请输入验证码');
      return ;
    }
    var params = {
      "apply_scenario": 1,
      "mobile_phone": state.phoneTextController.text,
      "sms_verify_code": state.verifyTextController.text,
      "sms_verify_token": state.smsData["token"],
      "sms_verify_type" : "signup",
      "usr_name": state.nameTextController.text,
      "mbertype": 0,
      "invite_mobile_phone": state.invitePhoneTextController.text,
      "channel_num_id": 99,
    };
    print(params);
    Api.applybecomekoc(params).listen((event) {
      if (event["code"] != 0) {
        toast(event["message"]);
      } else {
        toast("已提交申请");
        Get.toNamed(RouteConfig.mallLogin);
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    state.timer?.cancel();
  }

}